package lv.imho.client;

import java.util.Date;
import java.util.List;

import lv.imho.client.model.DataSourceTransferObject;
import lv.imho.client.model.DataTransferObject;
import lv.imho.client.services.DataService;
import lv.imho.client.services.DataServiceAsync;
import lv.imho.client.view.CalendarComponent;
import lv.imho.client.view.DataSourceListPanel;
import lv.imho.client.view.OverallStatusPanel;
import lv.imho.client.view.TextListPanel;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Home page entry point. Makes also functions of controller.
 * 
 * @author Igors Gulbinskisd
 */
public class Home implements EntryPoint {
		
	private final DataServiceAsync dataService = GWT.create(DataService.class);
	
	private List<DataSourceTransferObject> dataSourcesGlobal;
		
	public void onModuleLoad() {
		RootPanel.get("textListContainerLoader").setVisible(false);
		RootPanel.get("loader").setVisible(true);
		
		CalendarComponent calendarComponent = new CalendarComponent();
		calendarComponent.addValueChangeHandler(new ValueChangeHandler<Date>() {
			@Override
			public void onValueChange(final ValueChangeEvent<Date> event) {
				RootPanel.get("textListContainerLoader").setVisible(true);
				RootPanel.get("textListContainer").setVisible(false);				
				
				dataService.getData(dataSourcesGlobal, event.getValue(), new AsyncCallback<List<DataTransferObject>>() {				
					@SuppressWarnings("deprecation")
					@Override
					public void onSuccess(List<DataTransferObject> dataList) {												
						/* formatter not supported on UI side */
						String dateString = (event.getValue().getDate()) + "-"
							+ (event.getValue().getMonth() + 1) + "-"
							+ (event.getValue().getYear() + 1900);
						GWT.log("Data for " + dateString  + " successfully retrieved (length = " + dataList.size() + ")");
						
						RootPanel.get("overallStatusContainer").clear();
						RootPanel.get("overallStatusContainer").add(new OverallStatusPanel(dataList));
					
						RootPanel.get("textListContainerLoader").setVisible(false);
						RootPanel.get("textListContainer").setVisible(true);
						RootPanel.get("textListContainer").clear();
						RootPanel.get("textListContainer").add(new TextListPanel(dataList));						
					}
						
					@Override
					public void onFailure(Throwable caught) {
						GWT.log("Error on date highlighting");						
					}
				});
			}			
		});
		RootPanel.get("calendarContainer").add(calendarComponent);
		
		dataService.getDataSources(new AsyncCallback<List<DataSourceTransferObject>>(){			
			@Override
			public void onSuccess(final List<DataSourceTransferObject> dataSources) {
				GWT.log("Data sources successfully obtained (length = " + dataSources.size() + ")");								
				dataSourcesGlobal = dataSources;

				dataService.getData(dataSources, new Date(), new AsyncCallback<List<DataTransferObject>>() {				
					@Override
					public void onSuccess(List<DataTransferObject> dataList) {
						GWT.log("Initial data successfully obtained (length = " + dataList.size() + ")");
						
						/* add all UI components */
						DataSourceListPanel dataSourcePanel = new DataSourceListPanel(dataSources);
						RootPanel.get("dataSourceContainer").add(dataSourcePanel);						
						RootPanel.get("overallStatusContainer").add(new OverallStatusPanel(dataList));						
						RootPanel.get("textListContainer").add(new TextListPanel(dataList));
						
						RootPanel.get("loader").setVisible(false);
					}
						
					@Override
					public void onFailure(Throwable caught) {
						GWT.log("Error on obtaining initial data");
					}
				});
			}
			
			@Override
			public void onFailure(Throwable caught) {
				GWT.log("Error on obtaining data sources");
			}
		});		
	}
	
}
